package com.base.common.pagination;

import java.util.ArrayList;
import java.util.List;

import com.base.common.util.PageUtil;
import com.lovenote.common.global.GlobalDefine;


/**
 * Pagination
 * @author xingkong1221
 *
 */
public class Page {
	
	
	public Page() {
	}
	
	public Page(long total) {
		this(total, 1);
	}
	
	/**
	 * Construct the page object
	 * @param total	The total number of records
	 * @param pageNumber The current page number(start 1)
	 */
	public Page(long total, int pageNumber) {
		this.total = total;
		this.pageNumber = pageNumber;
	}
	
	public void initialize() {
		// Total number of page
		this.totalPage = PageUtil.getPageCount((int)total, pageSize);
		
		// Starting record number
		this.startIndex = PageUtil.getStartIndex(pageNumber, pageSize);
		
		// page number start
		int first = pageNumber - pagesDisplayed / 2;
		if (first < 1) {
			first = 1;
		}
		// page number length
		int length = 0;
		length = (first + pagesDisplayed - 1 <= this.totalPage) ? pagesDisplayed : totalPage - first + 1;
		
		// Add page number to the list
		for (int i = 0; i < length; i++) {
			this.pages.add(first + i);
		}
		
		// Add 1 page to the list
		if (!this.pages.isEmpty() && this.pages.get(0) != 1) {
			this.pages.add(0, 1);
		}
		
		// Add last page to the list
		if (!this.pages.isEmpty() && pages.get(this.pages.size() - 1) != this.totalPage) {
			this.pages.add(this.totalPage);
		}
		
		// Previous page
		this.hasPre = (pageNumber > 1) ? true : false;
		
		// Next page
		this.hasNext = (pageNumber < totalPage) ? true : false;
	}
	
	/**
	 * Is there previous page
	 */
	private boolean hasPre;
	
	/**
	 * Is there next page
	 */
	private boolean hasNext;
	
	/**
	 * Current page number(start 1)
	 */
	private int pageNumber;
	
	/**
	 * The total number of records
	 */
	private long total;
	
	/**
	 * Starting record number
	 */
	private int startIndex;
	
	/**
	 * The total number of pages
	 */
	private int totalPage;
	
	/**
	 * Default number of record per page
	 */
	private int pageSize = GlobalDefine.PAGER.DEFAULT_PAGE_SIZE;
	
	/**
	 * Default number of pages that displayed each time
	 */
	private int pagesDisplayed = GlobalDefine.PAGER.DEFAULT_PAGINATION_DISPLAYED; 
	
	/**
	 * The list of pages
	 */
	private List<Integer> pages = new ArrayList<Integer>();
	
	/**
	 * Sort field
	 */
	private String sort = "";
	
	/**
	 * Sort direction
	 */
	private String dir = "";

	public boolean isHasPre() {
		return hasPre;
	}

	public void setHasPre(boolean hasPre) {
		this.hasPre = hasPre;
	}

	public boolean isHasNext() {
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPagesDisplayed() {
		return pagesDisplayed;
	}

	public void setPagesDisplayed(int pagesDisplayed) {
		this.pagesDisplayed = pagesDisplayed;
	}

	public List<Integer> getPages() {
		return pages;
	}

	public void setPages(List<Integer> pages) {
		this.pages = pages;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}
}
